package com.trkj.zzplus.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.trkj.zzplus.R;
import com.trkj.zzplus.ui.news.LoadListView;
import com.trkj.zzplus.ui.news.NewsDetailActivity;
import com.trkj.zzplus.ui.news.NewsItemBean;
import com.yalantis.phoenix.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/4.
 */
public class MainUiFragment extends Fragment implements LoadListView.ILoadListener {
    //新闻数据
    private int[] imgs = {R.mipmap.news22, R.mipmap.news33, R.mipmap.zz_customs, R.mipmap.e2};
    private String[] newsSrcs = {"新浪新闻", "新华社", "澎湃新闻", "网易新闻"};
    private String[] newsTitles = {"德意志勇夺德劳内杯?", "3场都是大比分！", "横扫骑士?", "知乎果壳必有一战?"};
    private String newsType;
    private String[] topTitles = new String[]{
            "热门", "要闻", "时尚娱乐", "株洲时政",
            "株洲民生", "军事历史", "微话题", "爆料", "专题"
    };

    private LoadListView newsListView;
    private PullToRefreshView mPullToRefreshView;

    public MyNewsAdapter adapter;
    public View view;//

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.ui_fragment, container, false);
        newsListView = (LoadListView) view.findViewById(R.id.ui_newsList);
        newsListView.setInterface(this);
/*
        //得到上级传送的数据
        getSendData();*/

        listViewListener();
        //太阳样式下拉刷新
        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        newsListView.setAdapter(new MyNewsAdapter());
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        initAdapter();

        return view;
    }

    /* private void getSendData() {
         Bundle data = getArguments();
         newsType = data.getString("newsType");
     }
 */
    private void initAdapter() {
        adapter = new MyNewsAdapter();
        newsListView.setAdapter(adapter);
    }

    private void listViewListener() {
        newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               /* //传值
                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("newsType",newsType);
                intent.putExtras(bundle);
                startActivity(intent);*/
                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void OnLoad() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //通知ListView更新界面
                reChangeListView();
            }
        }, 1500);
        //获取更多数据

    }

    public void reChangeListView() {
        if (adapter == null) {
            newsListView = (LoadListView) view.findViewById(R.id.ui_newsList);
            newsListView.setInterface(this);
            adapter = new MyNewsAdapter();
            newsListView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();//数据变化时更新listView
        }
        //通知listView加载完毕
        newsListView.loadComplete();
    }

    class MyNewsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            //新闻数量,默认一次10条
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final NewsHolder newsHolder;
            //  List<NewsItemBean> news = new ArrayList<>();
            int n = (int) (Math.random() * 3 + 1);
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.ui_newslist_item, null);
                newsHolder = new NewsHolder();
                newsHolder.newsImg = (ImageView) convertView.findViewById(R.id.news_item_img);
                newsHolder.src = (TextView) convertView.findViewById(R.id.news_item_src);
                newsHolder.title = (TextView) convertView.findViewById(R.id.news_item_title);
                newsHolder.volume = (TextView) convertView.findViewById(R.id.news_item_volume);
                newsHolder.time = (TextView) convertView.findViewById(R.id.news_item_time);
                convertView.setTag(newsHolder);
            } else {
                newsHolder = (NewsHolder) convertView.getTag();
            }
            newsHolder.newsImg.setBackgroundResource(imgs[n]);
            newsHolder.src.setText(newsSrcs[n]);
            newsHolder.title.setText(newsTitles[n]);

            return convertView;
        }

        class NewsHolder {
            public ImageView newsImg;
            public TextView title;
            public TextView src;
            public TextView time;
            public TextView volume;
        }
    }

}